package com.baosight.sczx.zm.cn.service;
import com.baosight.innerinterface.service.ServiceDesignIn;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.cn.domain.ZMCNMzjc;
/**
 * 煤质监测上报管理
 */
public class ServiceZMCN60 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "50");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
//		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
//		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
//		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
//		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		return outInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {	
		if(null != inInfo.getBlock("inqu_status")){
		String fdStatus = inInfo.getBlock("inqu_status").getCellStr(0, "fdStatus");
		String fdMyNum = inInfo.getBlock("inqu_status").getCellStr(0, "fdMyNum");
		String fdHyDate = inInfo.getBlock("inqu_status").getCellStr(0, "fdHyDate");
		if(null != fdStatus && !("").equals(fdStatus)){
			StaticTools.setQueryParameters(inInfo, "fdStatus", fdStatus, true);//添加查询条件 
		}
		if(null != fdMyNum && !("").equals(fdMyNum)){
			StaticTools.setQueryParameters(inInfo, "fdMyNum", fdMyNum, true);//添加查询条件 
		}
		if(null != fdHyDate && !("").equals(fdHyDate)){
			StaticTools.setQueryParameters(inInfo, "fdHyDate", fdHyDate, true);//添加查询条件 
		}
		}		
		EiInfo outInfo = super.query(inInfo, "ZMCNMzjc.query", new ZMCNMzjc());
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		return outInfo;
	}	
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNMzjc _dom = new ZMCNMzjc();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String fdReportTime = StaticTools.getDateTime();
		String fdStatus = "草拟";
		String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
		String fdReportDepartment =  (String) UserSession.getInSessionProperty("orgName");
		String fdReportDepartmentId =  (String) UserSession.getInSessionProperty("orgCode");
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdReportPerson(fdReportPerson);
			_dom.setFdReportTime(fdReportTime);
			_dom.setFdReportDepartmentId(fdReportDepartmentId);
			_dom.setFdReportDepartment(fdReportDepartment);
			_dom.setFdStatus(fdStatus);
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_MZJC"));
			this.dao.insert("ZMCNMzjc.insert", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNMzjc _dom = new ZMCNMzjc();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String fdReportTime = StaticTools.getDateTime();
		String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
		String fdReportDepartment =  (String) UserSession.getInSessionProperty("orgName");
		String fdReportDepartmentId =  (String) UserSession.getInSessionProperty("orgCode");
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			_dom.setFdReportPerson(fdReportPerson);
			_dom.setFdReportTime(fdReportTime);
			_dom.setFdReportDepartmentId(fdReportDepartmentId);
			_dom.setFdReportDepartment(fdReportDepartment);
			this.dao.update("ZMCNMzjc.update", _dom);
			}else{
				outInfo = this.query(inInfo);
				outInfo.setStatus(-1);
				outInfo.setMsg("只能修改草拟状态的记录!"); 
				return outInfo;
			}
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNMzjc _dom = new ZMCNMzjc();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			this.dao.delete("ZMCNMzjc.delete", _dom);
		    }else{
				outInfo = this.query(inInfo);
				outInfo.setStatus(-1);
				outInfo.setMsg("只能删除草拟状态的记录!"); 
				return outInfo;
			}
	     }
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p> 
	 */
	public EiInfo report(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNMzjc _dom = new ZMCNMzjc();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
		    if(validate(_dom)){
		    	
		    	//给销售管理接口传数据
		    	ServiceDesignIn sd=new ServiceDesignIn();
		    	sd.syncQualityInfo(inInfo);
		    this.dao.update("ZMCNMzjc.report", _dom);
		    }else{
				outInfo = this.query(inInfo);
				outInfo.setStatus(-1);
				outInfo.setMsg("只能上报草拟状态的记录!"); 
				return outInfo;
			}
		    }
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	
	private boolean validate(ZMCNMzjc _dom){
		  String fdStatus = _dom.getFdStatus();
		  if("草拟".equals(fdStatus)){
			 return true; 
		  }else
			  return false;
		  
	}
	
} 